package com.guoqi.clickevent

import android.content.Context
import android.graphics.*
import android.view.MotionEvent
import android.view.View

/**
 *  作者    GUOQI
 *  时间    2019-11-07 14:26
 *  描述
 */
class PathView(context: Context) : View(context) {
    //画笔
    private var paint: Paint? = null

    //画布
    private var cacheCanvas: Canvas? = null

    //位图
    private var cachebBitmap: Bitmap? = null

    //图片保存路径
    private var path: Path? = null

    private var cur_x: Float = 0.toFloat()
    private var cur_y: Float = 0.toFloat()

    private var w = 0
    private var h = 0

    private lateinit var rectPaint: Paint
    private lateinit var textPaint: Paint
    private lateinit var rectF: RectF

    init {
        init()
    }

    /**
     * 初始化
     */
    private fun init() {
        w = SystemUtils.getScreenWidth(context)
        h = SystemUtils.getScreenHeight(context) - SystemUtils.dp2px(context, 70f)

        //背景矩形
        rectPaint = Paint()
        rectPaint.style = Paint.Style.STROKE
        rectPaint.color = Color.GRAY
        rectPaint.pathEffect = DashPathEffect(floatArrayOf(15f, 5f), 0f)
        rectF = RectF(200f, 150f, (w - 200).toFloat(), (h - 150).toFloat())

        //提示文字
        textPaint = Paint()
        textPaint.style = Paint.Style.FILL
        textPaint.strokeWidth = 10f
        textPaint.textSize = 50f
        textPaint.color = Color.GRAY
        textPaint.textAlign = Paint.Align.CENTER

        //设置画笔
        paint = Paint()
        paint!!.isAntiAlias = true
        paint!!.strokeWidth = 15f
        paint!!.style = Paint.Style.STROKE
        paint!!.strokeJoin = Paint.Join.ROUND
        paint!!.strokeCap = Paint.Cap.ROUND
        paint!!.isDither = true
        path = Path()

        //创建位图
        cachebBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
        //用自定义位图构建画布
        cacheCanvas = Canvas(cachebBitmap!!)
        //设置画布为白色
        cacheCanvas!!.drawColor(Color.WHITE)

    }

    /**
     * 清除画板，重置画笔
     */
    fun clear() {
        if (cacheCanvas != null) {
            paint!!.color = Color.TRANSPARENT
            cacheCanvas!!.drawPaint(paint!!)
            paint!!.color = Color.BLACK
            cacheCanvas!!.drawColor(Color.WHITE)

            textPaint.color = Color.GRAY
            rectPaint.color = Color.GRAY
            invalidate()
        }
    }

    fun clearTip() {
        textPaint.color = Color.TRANSPARENT
    }

    fun clearRectF() {
        rectPaint.color = Color.TRANSPARENT
        cacheCanvas!!.drawPaint(rectPaint)
        invalidate()
    }

    override fun onDraw(canvas: Canvas) {
        canvas.drawBitmap(cachebBitmap!!, 0f, 0f, null)
        canvas.drawPath(path!!, paint!!)

        //矩形边框
        canvas.drawRect(rectF, rectPaint)

        //提示文字
        val text = "等待显示执行手势..."
        //计算baseline
        val fontMetrics = textPaint.fontMetrics
        val distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom
        val baseline = rectF.centerY() + distance
        canvas.drawText(text, rectF.centerX(), baseline, textPaint)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {

        var curW = if (cachebBitmap != null) cachebBitmap!!.width else 0
        var curH = if (cachebBitmap != null) cachebBitmap!!.height else 0
        if (curW >= w && curH >= h) {
            return
        }

        if (curW < w) curW = w
        if (curH < h) curH = h

        val newBitmap = Bitmap.createBitmap(curW, curH, Bitmap.Config.ARGB_8888)
        val newCanvas = Canvas()
        newCanvas.setBitmap(newBitmap)
        if (cachebBitmap != null) {
            newCanvas.drawBitmap(cachebBitmap!!, 0f, 0f, null)
        }
        cachebBitmap = newBitmap
        cacheCanvas = newCanvas
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {

        val x = event.x
        val y = event.y

        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                cur_x = x
                cur_y = y
                path!!.moveTo(cur_x, cur_y)
                //按下的时候把中间文字清除
                clearTip()
            }
            MotionEvent.ACTION_MOVE -> {
                path!!.quadTo(cur_x, cur_y, x, y)
                cur_x = x
                cur_y = y
            }
            MotionEvent.ACTION_UP -> {
                path!!.reset()
                clear()
            }
        }
        invalidate()
        return true
    }

}